<?php
/**
 * Eka Framework 0.3.0
 *
 * @copyright Copyright (c) 2008 Alexey Yu. Chuprina <656b61@gmail.com> (http://php-eka.googlecode.com)
 * @license http://www.opensource.org/licenses/mit-license.php MIT License
 *
 * @version $Id$
 */

require_once 'Eka/Application/View/Interface.php';

/**
 * Eka_Application_View
 *
 * @package Application
 * @subpackage View
 */
class Eka_Application_View implements Eka_Application_View_Interface
{
	/**
	 * @var Eka_Application_Controller
	 */
	protected $controller;
	
	/**
	 * Template file name
	 *
	 * @var string
	 */
	protected $file;
	
	/**
	 * Layout file name
	 *
	 * @var string
	 */
	protected $layout;
	
	/**
	 * @var bool
	 */
	protected $useLayout = true;
	
	/**
	 * @var array
	 */
	protected $variables = array();
	
	/**
	 * Only layout variables
	 *
	 * @var array
	 */
	protected $layoutVars = array();

	/**
	 * Constuctor
	 *
	 * @param Eka_Application_Controller $controller
	 * @param string $file Template file
	 * @param string $layout Layout file
	 */
	public function __construct(Eka_Application_Controller $controller, $file, $layout = null)
	{
		$this->controller = $controller;
		$this->file = $file;
		$this->layout = $layout;
	}

	/**
	 * Assign a variable
	 *
	 * @param string $key Variable name
	 * @param mixed $val Variable value
	 */
	public function __set($key, $val)
	{
		$this->variables[$key] = $val;
	}

	/**
	 * Get assigned variable value
	 *
	 * @param string $key Variable name
	 * @return mixed
	 */
	public function __get($key)
	{
		return isset($this->variables[$key]) ? $this->variables[$key] : null;
	}

	/**
	 * Allows testing with empty() and isset() to work
	 *
	 * @param string $key Variable name
	 * @return bool
	 */
	public function __isset($key)
	{
		return isset($this->variables[$key]);
	}

	/**
	 * Allows unset() on object properties to work
	 *
	 * @param string $key Variable name
	 */
	public function __unset($key)
	{
		if (isset($this->variables[$key])) {
			unset($this->variables[$key]);
		}
	}

	/**
	 * Assign variables
	 *
	 * @param string|array $key Variable name or array of key => value pairs
	 * @param mixed $value If assigning a named variable, use this as the value
	 * @return Eka_Application_View
	 */
	public function set($key, $value = null)
	{
		if (is_array($key)) {
			foreach ($key as $k => $v) {
				$this->__set($k, $v);
			}
		} else {
			$this->__set($key, $value);
		}
		return $this;
	}

	/**
	 * Get assigned variable value
	 *
	 * @param string $key Variable name
	 * @return mixed
	 */
	public function get($key)
	{
		return $this->__get($key);
	}

	/**
	 * Use layout or not
	 *
	 * @param bool $is
	 * @return bool
	 */
	public function useLayout($is = null)
	{
		if (null !== $is) {
			$old = $this->useLayout;
			$this->useLayout = (bool)$old;
			return $old;
		} else {
			return $this->useLayout;
		}
	}
	
	/**
	 * Assign layout variables
	 *
	 * @param string|array $key Variable name or array of key => value pairs
	 * @param mixed $value (Optional) If assigning a named variable, use this as the value
	 * @return Eka_Application_View
	 */
	public function setLayoutVar($key, $value = null)
	{
		if (is_array($key)) {
			foreach ($key as $k => $v) {
				$this->layoutVars[$k] = $v;
			}
		} else {
			$this->layoutVars[$key] = $value;
		}
		return $this;
	}
	
	/**
	 * Get assigned layout variable value
	 *
	 * @param string $key Variable name
	 * @return mixed
	 */
	public function getLayoutVar($key)
	{
		return isset($this->layoutVars[$key]) ? $this->layoutVars[$key] : null;
	}
	
	/**
	 * Processes a view template and returns the output
	 *
	 * @return string
	 */
	public function execute()
	{
		foreach ($this->variables as $__n => &$__v) {
			$$__n = &$__v;
		}
		
		//$errorLevel = error_reporting(E_ALL ^ E_NOTICE ^ E_WARNING);
		
		ob_start();
		include_once $this->file;
		$_CONTENT_ = ob_get_clean();
		
		if (null !== $this->layout && $this->useLayout) {
			if (count($this->layoutVars) !== 0) {
				foreach ($this->layoutVars as $__n => &$__v) {
					$$__n = &$__v;
				}
			}
			ob_start();
			include_once $this->layout;
			return ob_get_clean();
		} else {
			return $_CONTENT_;
		}
		
		//error_reporting($errorLevel);
	}
	
	/**
	 * @return Eka_Application_Controller
	 */
	public function getController()
	{
		return $this->controller;
	}
	
	/**
	 * @return Eka_Application
	 */
	public function getApplication()
	{
		return $this->controller->getApplication();
	}

	/**
	 * @return Eka_Application_Request
	 */
	public function getRequest()
	{
		return $this->controller->getRequest();
	}

	/**
	 * @return Eka_Application_Response
	 */
	public function getResponse()
	{
		return $this->controller->getResponse();
	}
}
